#ifndef TCP_SERVER_LOGFILE_H
#define TCP_SERVER_LOGFILE_H

#include "../thread/Mutex.h"
#include "../Types.h"
#include "../noncopyable.h"

#include <memory>


namespace FileUtils {
    class AppendFile;
}

class LogFile : muduo::noncopyable {
public:
    LogFile(const string &basename,
            off_t rollSize,
            bool threadSafe = true,
            int flushInterval = 3,
            int checkEveryN = 1024);

    ~LogFile();

    void append(const char *logline, int len);

    void flush();

    bool rollFile();

private:
    void append_unlocked(const char *logline, int len);

    static string getLogFileName(const string &basename, time_t *now);

    const string basename_;
    const off_t rollSize_;
    const int flushInterval_;
    const int checkEveryN_;

    int count_;

    std::unique_ptr<MutexLock> mutex_;
    time_t startOfPeriod_;
    // 启动时间
    time_t lastRoll_;
    // 上一次切换日志文件时间
    time_t lastFlush_;
    // 上一次写入日志文件时间
    std::unique_ptr<FileUtils::AppendFile> file_;

    const static int kRollPerSeconds_ = 60 * 60 * 24;
};


#endif //TCP_SERVER_LOGFILE_H
